﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using Gma.System.MouseKeyHook;
using Timer = System.Timers.Timer;
using System.Threading.Tasks;

namespace EasyKaPai
{
    public class KaiPaiProcessor
    {
        Color _red = Color.FromArgb(255, 223, 19, 21);
        Color _yellow = Color.FromArgb(255, 222, 215, 0); //Color [A=255, R=192, G=182, B=0]
        Color _blue = Color.FromArgb(255, 51, 54, 150);
        Color _ready = Color.FromArgb(255, 74, 24, 148);

        private Color _targetColor;
        private readonly IDictionary<char, Color> _keyToColor;
        private IKeyboardMouseEvents m_GlobalHook;
        private bool _processing;
        private Timer timer = new Timer(50);


        public KaiPaiProcessor()
        {
            _keyToColor = new Dictionary<char, Color>()
            {
                {'x',_red},
                {'e',_yellow},
                {'c',_blue },
            };
            timer = new Timer(50);


        }


        public void Start()
        {
            m_GlobalHook = Hook.GlobalEvents();

            m_GlobalHook.KeyPress += GlobalHookKeyPress;
            m_GlobalHook.MouseMoveExt += M_GlobalHook_MouseMoveExt;
        }

        private void M_GlobalHook_MouseMoveExt(object sender, MouseEventExtArgs e)
        {
            
        }

        public void Stop()
        {
            m_GlobalHook.KeyPress -= GlobalHookKeyPress;
            m_GlobalHook.Dispose();
            m_GlobalHook = null;
        }

        private void GlobalHookKeyPress(object sender, KeyPressEventArgs e)
        {
            if (_processing)
                return;

            if (_keyToColor.Keys.Contains(e.KeyChar) 
                && Win32.GetPixelColor(1095, 1311) == _ready)
            {
                e.Handled = false;


                Task.Factory.StartNew(() =>
                {
                    Task.Delay(100).Wait();
                    SendKey(Win32.ScanCodeShort.KEY_W);
                    Task.Delay(2).Wait();
                    SendKeyUp(Win32.ScanCodeShort.KEY_W);

                    _targetColor = _keyToColor[e.KeyChar];
                    _processing = true;

                    for (int i = 0; i < 80; i++)
                    {
                        Task.Delay(50).Wait();

                        var color = Win32.GetPixelColor(1095, 1311);
                        if (color == _targetColor)
                        {
                            SendKey(Win32.ScanCodeShort.KEY_W);
                            Task.Delay(2).Wait();
                            SendKeyUp(Win32.ScanCodeShort.KEY_W);
                            break;
                        }
                    }

                    _processing = false;
                });

                
            }
        }



        private static uint SendKey(Win32.ScanCodeShort key1)
        {
            var inputs = new[]
                {
                    new Win32.INPUT()
                    {
                        type = Win32.InputType.KEYBOARD,
                        U = new Win32.InputUnion
                        {
                            ki = new Win32.KEYBDINPUT
                            {
                                wScan = key1,
                                dwExtraInfo = UIntPtr.Zero,
                                dwFlags =  Win32.KEYEVENTF.SCANCODE
                            }
                        }
                    }
                };
            return Win32.SendInput((uint)inputs.Length, inputs, Win32.INPUT.Size);
        }

        private static uint SendKeyUp(Win32.ScanCodeShort key)
        {
            var inputs = new[]
            {
                new Win32.INPUT()
                {
                    type = Win32.InputType.KEYBOARD,
                    U = new Win32.InputUnion
                    {
                        ki = new Win32.KEYBDINPUT
                        {
                            wScan = key,
                            dwExtraInfo = UIntPtr.Zero,
                            dwFlags = Win32.KEYEVENTF.SCANCODE | Win32.KEYEVENTF.KEYUP
                        }
                    }
                }
            };
            return Win32.SendInput((uint)inputs.Length, inputs, Win32.INPUT.Size);
        }
    }
}
